function authenticate() {
  const pwd = document.getElementById('pwd').value;
  fetch('/admin/login', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ password: pwd }),
    credentials: 'same-origin' // 重要：携带cookie
  })
  .then(res => res.json())
  .then(data => {
    if (data.success) {
      document.getElementById('auth').style.display = 'none';
      document.getElementById('uploader').style.display = 'block';
    } else {
      alert('Invalid password!');
    }
  });
}


function handleUpload(file) {
  const item = document.createElement('div');
  item.className = 'list-group-item';
  item.innerHTML = `
      <div>${file.name}</div>
      <div class="progress mt-2">
        <div class="progress-bar" role="progressbar"></div>
      </div>
    `;
  document.getElementById('uploadList').appendChild(item);

  if (file.size <= 100 * 1024) {
    // 直接上传
    uploadDirect(file, item.querySelector('.progress-bar'));
  } else {
    // 分块上传 
    uploadChunked(file, item.querySelector('.progress-bar'));
  }
}


function updateProgress(e, progressBar) {
  const percent = Math.round(e.loaded / e.total * 100);
  progressBar.style.width = `${percent}%`;
  progressBar.textContent = `${percent}%`;
}